package com.yiyi.controller;

import com.yiyi.form.SearchQuestionForm;
import com.yiyi.service.ChoiceService;
import com.yiyi.form.AddChoiceForm;
import com.yiyi.vo.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author yiyi
 * @date 2021/5/30
 *
 * 1、新增题目
 * 2、删除题目
 * 3、对全部题目进行分页查询
 * 4、按公司id查询题目列表
 * 5、按题目id查询单道题目
 * 6、按题目id列表查询多道题目
 *
 * 注意：传回前端数据中tag仍为字符串形式
 */
@RestController
@RequestMapping("/choice")
@Api(tags = {"选择题操作接口"})
public class ChoiceController {

    @Autowired
    private ChoiceService choiceService;

    /**
     * 新增选择题操作，传入新增选择题表单
     *
     * @param addChoiceForm
     * @return
     * */
    @ApiOperation(value = "新增题目")
    @PostMapping("/save")
    public R save(@RequestBody AddChoiceForm addChoiceForm) {
        int flag = choiceService.save(addChoiceForm);
        if (flag != 0) {
            return R.ok();
        }
        return R.fail();
    }

    /**
     * 删除题目
     *
     * @param id
     * @return
     * */
    @ApiOperation(value = "删除题目")
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable("id") String id) {
        int flag = choiceService.delete(id);
        if (flag != 0) {
            return R.ok();
        }
        return R.fail();
    }

    /**
     * 分页查询
     *
     * @param searchQuestionForm
     * @return
     * */
    @ApiOperation(value = "分页查询题目")
    @PostMapping("/findByPage")
    public R findByPage(@RequestBody SearchQuestionForm searchQuestionForm) {
        return R.ok(choiceService.findByPage(searchQuestionForm));
    }

    /**
     * 按公司id查询题目列表
     *
     * @param companyId
     * @return
     * */
    @ApiOperation(value = "根据公司id查询多道题目")
    @GetMapping("findByCompanyId/{companyId}")
    public R findByCompanyId(@PathVariable("companyId") String companyId) {
        return R.ok(choiceService.findByCompanyId(companyId));
    }

    /**
     * 根据id查询单道题目
     *
     * @param id
     * @return
     * */
    @ApiOperation(value = "根据id查询单道题目")
    @GetMapping("/findById/{id}")
    public R findById(@PathVariable("id") String id) {
        return R.ok(choiceService.findOneById(id));
    }

    /**
     * 按题目id列表查询多道题目
     *
     * @param idList
     * @return
     * */
    @ApiOperation(value = "根据题目id列表查询多道题目")
    @PostMapping("/findListById")
    public R findListById(@RequestBody List<String> idList) {
        return R.ok(choiceService.findListByIds(idList));
    }
}
